package com.controller;

import com.pojo.Member;
import com.pojo.OrderForm;
import com.pojo.Product;
import com.pojo.Result;
import com.server.OrderhandingService;
import com.server.impl.AmountServiceImpl;
import com.server.impl.OrderServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description：交易管理
 */
@Controller
@RequestMapping("transaction")
public class TransactionController {

    @Resource
    OrderServiceImpl orderService;
    @Resource
    AmountServiceImpl amountService;
    @Resource
    OrderhandingService orderhandingService;

    @RequestMapping(value ="/message",method = RequestMethod.GET)
    public String index1(){
        return "pages/transaction" ;
    }

    @RequestMapping(value ="/order",method = RequestMethod.GET)
    public String index2(){
        return "pages/Order_Chart" ;
    }

    //订单管理
    @RequestMapping(value ="/orderform",method = RequestMethod.GET)
    public String orderform(Model model){
        List<OrderForm> orderList= orderService.getorderList();
        model.addAttribute("orderList",orderList);

        return "pages/Orderform" ;
    }
    @RequestMapping("/findorder")
    @ResponseBody
    public ModelAndView findorder(String ordername) {
        //1.调用service方法
        List<OrderForm> orderList = orderService.findorder(ordername);
        //2.实例化一个ModelAndView对象
        ModelAndView mv=new ModelAndView();
        mv.addObject("orderList",orderList);
        mv.setViewName("/pages/Orderform");
        return mv;

    }

    //订单金额
    @RequestMapping(value ="/amount",method = RequestMethod.GET)
    public String amount(Model model){
        List<OrderForm> amountList= amountService.getamountList();
        model.addAttribute("amountList",amountList);
        return "pages/Amounts" ;
    }

    //订单状态更新
    @RequestMapping(value ="/OrderStatus",method = RequestMethod.GET)
    @ResponseBody
    public Result modifyOrderStatus(int status,String user_message,String order_code,Model model){
        Result result = new Result();
        //当前时间日期
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        Date d = new Date(System.currentTimeMillis());
        //int count=orderhandingService.modifyOrderStatus(status, user_message,order_code);
        int count=0;
        if (status==0){
            System.out.println("已付款");
        }else if (status==1){
            count=orderhandingService.modifyOrderStatus_delivery_date(status, user_message, d, order_code);
            System.out.println("待收货");
        }else if (status==2){
            count=orderhandingService.modifyOrderStatus_confirm_date(status, d, order_code);
            System.out.println("已完成");
        }
        if (count>0){
            System.out.println("状态更新成功");
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        return result;
    }



    //订单处理
    @RequestMapping(value ="/orderhanding",method = RequestMethod.GET)
    public String orderhanding(Model model){
        List<OrderForm> orderhandingList= orderhandingService.getorderhandingList();
        model.addAttribute("orderhandingList",orderhandingList);
        return "pages/Order_handling" ;
    }
    @RequestMapping("/findorderhanding")
    @ResponseBody
    public ModelAndView findorderhanding(String orderhandingname) {
        //1.调用service方法
        List<OrderForm> orderhandingList = orderhandingService.findorderhanding(orderhandingname);
        //2.实例化一个ModelAndView对象
        ModelAndView mv=new ModelAndView();
        mv.addObject("orderhandingList",orderhandingList);
        mv.setViewName("/pages/Order_handling");
        return mv;

    }
    /*@RequestMapping(value ="/edit2",method = RequestMethod.GET)
    @ResponseBody
    public Result updatehanding(@Param("order_id") Integer order_id, Model model, HttpSession session){
        Result result = new Result();
        if (session.getAttribute("handing")!=null){
            session.removeAttribute("handing");
        }

        OrderForm handing=orderhandingService.selecthandingById(order_id);
        System.out.println(handing);

        session.setAttribute("handing",handing);

        if (handing!=null){
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);

        return result;

    }*/

   /* @RequestMapping(value ="/updatehanding",method = RequestMethod.GET)
    @ResponseBody
    public Result updatehanding(@Param("order_id") int order_id,@Param("order_code")int order_code,@Param("order_receiver")String order_receiver,@Param("sell_sum_price")int sell_sum_price,@Param("sell_amounts")int sell_amounts,@Param("mobile")String mobile,HttpSession session){
        int member_id,String member_name,String member_nickname,String member_gender,String member_phone,String member_email
        Result result = new Result();
        int count=orderhandingService.updatehanding(order_id, order_code, order_receiver, sell_sum_price, sell_amounts, mobile);
        session.removeAttribute("handing");
        if (count>0){
            System.out.println("修改用户成功");
            result.setSuccess(true);
            return result;
        }
        System.out.println("修改用户失败");
        result.setSuccess(false);

        return result;
    }
*/
    /*@RequestMapping(value ="/delhanding",method = RequestMethod.GET)
    public String delhanding(int order_id,Model model){
        orderhandingService.delhanding(order_id);
        return "pages/Order_handling";
    }*/
}
